package com.yanqu.road.server.manger.activity.mammonhall;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.mammonhall.MammonHallTimeInfo;
import com.yanqu.road.entity.activity.mammonhall.MammonHallUserData;
import com.yanqu.road.entity.activity.mammonhall.MammonHallWorship;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.activity.MammonHallBusiness;
import com.yanqu.road.pb.activity.MammonHallProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.mammonhall.MammonHallModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MammonHallMgr extends TempMgr {

    public final static int activityChildType = 32;

    private static ActivityInfo activityInfo;

    /**
     * 榜单开放配置
     */
    static List<MammonHallTimeInfo> timeInfoList;

    /**
     * 榜单膜拜奖励：元宝数量
     */
    public static int MAMMON_HALL_RANK_WORSHIP_REWARD = 50;

    /**
     * 榜单数据统计时间
     */
    public static String MAMMON_HALL_DATA_TIME_RANGE = "2020.7.15.0#2021.7.15.0";

    /**
     *
     */
    public static int MAMMON_HALL_RANK_NUM = 20;

    @Override
    public boolean init() throws Exception {
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void reloadActivityData() {
        ActivityInfo inTimeActivity = null;
        List<MammonHallTimeInfo> tempTimeInfoList = new ArrayList<>();
        for (ActivityInfo activityInfo : NormalActivityMgr.getOpenActivityInfoList(eActivityType.PlatformActivity.getValue(), activityChildType)) {
            if (ActivityMgr.activityInShowTime(activityInfo)) {
                inTimeActivity = activityInfo;
                break;
            }
        }
        if (inTimeActivity != null) {
            // 活动配置
            Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(inTimeActivity.getActivityId());
            if(activityConfigMap != null){
                MAMMON_HALL_RANK_WORSHIP_REWARD = activityConfigMap.get("MAMMON_HALL_RANK_WORSHIP_REWARD").getIntValue();
                MAMMON_HALL_DATA_TIME_RANGE = activityConfigMap.get("MAMMON_HALL_DATA_TIME_RANGE").getValue();
                if(activityConfigMap.containsKey("MAMMON_HALL_RANK_NUM")){
                    MAMMON_HALL_RANK_NUM = activityConfigMap.get("MAMMON_HALL_RANK_NUM").getIntValue();
                }
            }
            tempTimeInfoList = MammonHallBusiness.getTimeInfoList(inTimeActivity.getActivityId());
        }
        timeInfoList = tempTimeInfoList;
        activityInfo = inTimeActivity;
        playerReloadActivity();
        getLogger().info("reload MammonHall end");
    }


    private static void playerReloadActivity() {
        List<GamePlayer> playerList = GamePlayerMgr.getCopyPlayerList();
        for(GamePlayer onlinePlay : playerList){
            onlinePlay.getModule(MammonHallModule.class).syncActivity();
        }
    }

    /**
     * 榜单是否已开发
     */
    public static boolean inOpenTime(int type) {
        if(activityInfo == null){
            return false;
        }
        MammonHallTimeInfo hallTimeInfo = null;
        for (MammonHallTimeInfo timeInfo : timeInfoList) {
            if(timeInfo.getType() == type){
                hallTimeInfo = timeInfo;
                break;
            }
        }
        if(hallTimeInfo != null){
            int passSecond = DateHelper.getCurrentSecond() - (int)activityInfo.getBeginTime();
            if(passSecond >= hallTimeInfo.getStartTime()){
                return true;
            }
        }
        return false;
    }

    public static MammonHallProto.MammonHallUserDataSyncMsg.Builder getUserDataSyncBuilder(MammonHallUserData userData) {
        MammonHallProto.MammonHallUserDataSyncMsg.Builder syncMsg = MammonHallProto.MammonHallUserDataSyncMsg.newBuilder();
        syncMsg.setDataTimeRange(MAMMON_HALL_DATA_TIME_RANGE);
        for (MammonHallTimeInfo timeInfo : timeInfoList) {
            MammonHallProto.MammonHallRankOpenTemp.Builder builder = MammonHallProto.MammonHallRankOpenTemp.newBuilder();
            builder.setType(timeInfo.getType());
            builder.setStartTime(timeInfo.getStartTime());
            builder.setEndTime(timeInfo.getEndTime());
            syncMsg.addOpenTime(builder);
        }
        for (Map.Entry<Integer, MammonHallWorship> worshipEntry : userData.getWorshipMap().entrySet()) {
            MammonHallProto.MammonHallRankWorshipTemp.Builder builder = MammonHallProto.MammonHallRankWorshipTemp.newBuilder();
            builder.setType(worshipEntry.getKey());
            builder.setWorship(worshipEntry.getValue().isWorship());
            syncMsg.addWorship(builder);
        }
        return syncMsg;
    }

    public static int getActivityBeginTime(){
        if(activityInfo == null){
            return 0;
        }
        return (int)activityInfo.getBeginTime();
    }

    public static List<MammonHallTimeInfo> getTimeInfoList() {
        return timeInfoList;
    }
}
